package com.hyy.blogtest.controller;

import com.hyy.blogtest.common.pojo.dadaobject.UserInfo;
import com.hyy.blogtest.common.pojo.request.RegisterRequest;
import com.hyy.blogtest.common.pojo.request.UserLoginRequest;
import com.hyy.blogtest.common.pojo.response.RegisterResponse;
import com.hyy.blogtest.common.pojo.response.UserInfoResponse;
import com.hyy.blogtest.common.pojo.response.UserLoginResponse;
import com.hyy.blogtest.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource(name = "userServiceImpl")
    private UserService userService;

    //根据用户ID 查看用户信息
    @PostMapping("/getUserInfo")
    public UserInfoResponse getUserInfo(@NotNull Integer userId) {
            //NotNull来源于 依赖：spring-boot-starter-validation
        return userService.selectById(userId);
    }

    @PostMapping("/login")////需要加入@Validated注解，spring在能校验对象中的参数
    public UserLoginResponse login(@Validated @RequestBody UserLoginRequest userLoginRequest) {
        log.info("用户登录，用户名：{}", userLoginRequest.getUsername());
        return userService.login(userLoginRequest);
    }

    @GetMapping("/getAuthorInfo")
    public UserInfoResponse getAuthorInfo(@NotNull Integer blogId){
        log.info("根据当前博客，找到该博客作者的信息，博客id={}", blogId);
        return userService.getAuthorInfo(blogId);
    }

    @PostMapping("/register")
    public RegisterResponse register(@Validated @RequestBody RegisterRequest request){
        log.info("用户进行注册");
        return userService.register(request);
    }



}
